import MoviesModel from "../models/movies";

class Movies {
  constructor() {
  }

  async getMovies (req, res, next) {
    const { vague_name } = req.query;
    try {
      // 至少2个词
      if (!vague_name) {
        throw new Error("缺少参数:vague_name");
      } else if (vague_name.length < 2) {
        throw new Error("搜索参数至少两个词");
      }
      const searchString = new RegExp(vague_name);

      const moviesSearchData = await MoviesModel.find({ name: searchString }).limit(5);
      // const moviesSeachData = await MoviesModel.find({ name: seachString });
      res.send({
        code: 200,
        message: '搜索成功',
        result: moviesSearchData
      });
    } catch (err) {
      res.send({
        code: 0,
        message: '搜索失败',
        result: err.message
      });
    }

  }
}

export default new Movies();